package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pojo.Checkout;
import com.pojo.Commodity;
import com.pojo.Commoditylog4j;
import com.pojo.Vip;
import com.service.CommodityService;
import com.service.Log4jService;
import com.service.VipService;
import com.util.DateUtil;
import com.util.LayuiTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

@Transactional
@Controller
@RequestMapping("/log")
public class Log4jController {
    @Autowired
    public Log4jService log4jService;
    @Autowired
    public CommodityService commodityService;
    @Autowired
    public VipService vipService;


    //查询结算清单
    @RequestMapping("/checkoutList")
    @ResponseBody
    public String checkoutList(){
        return JSON.toJSONString(LayuiTable.tableData(log4jService.checkoutSelect(null).size(),
                log4jService.checkoutSelect(null)), SerializerFeature.DisableCircularReferenceDetect);
    }

//    添加清单商品
    @RequestMapping("/checkoutAdd")
    @ResponseBody
    public String checkoutAdd(@RequestParam("sid") Integer sid,@RequestParam("quantity") Double quantity){
        Integer cid= (Integer) commodityService.sellSelect(sid,null).get(0).get("cid");
        if (log4jService.checkoutInsert(new Checkout(cid,quantity))==1){
            return "ture";
        }else {
            return "false";
        }
    }

    //删除清单商品
    @RequestMapping("/checkoutDel")
    @ResponseBody
    public String checkoutDel(@RequestParam("cid") Integer cid){
        if (log4jService.checkoutDelCid(cid)==1){
            return "ture";
        }else {
            return "false";
        }
    }

    //退货
    @RequestMapping("/dellogInsert")
    @ResponseBody
    public String dellogInsert(@RequestParam("delsid") Integer delsid,@RequestParam("delquantity") Double delquantity){
        List<Map<String,Object>> commoditylog4js=log4jService.commoditylogSelect(null,null,null,null,null);
        Integer cid= (Integer) commodityService.sellSelect(delsid,null).get(0).get("cid");
        Commodity commoditys=commodityService.commodityFindByCid(cid).get(0);
        BigDecimal price=commoditys.getCprice();
        BigDecimal sprice=commoditys.getSprice();
        String mlittle=commoditys.getMids().getMlittle();
        String hiredate=DateUtil.getCurrentDate();
        Commoditylog4j commoditylog4j=new Commoditylog4j(null,cid,price,sprice,delquantity,mlittle,hiredate,1);
        int ci=log4jService.commoditylogInsert(commoditylog4j);
        System.out.println(ci);
        if (ci==1){
            return "ture";
        }else {
            return "false";
        }
    }

//    通过会员积分决定优惠
@RequestMapping("/reduced")
@ResponseBody
public double reduced (@RequestParam("vid") String vid){
    double reduced=1;
        if (vid.equals("无")==true){
            reduced= 1;
        }else{
            List<Vip> vips=vipService.vipSelectPhone(vid);
            if (vips.size()==0){
                vips=vipService.vipSelectVid(Integer.valueOf(vid));
            }
            if (vips.size()==1){
                Integer integer=vips.get(0).getVipaccounts().getIntegral();
                if (integer<200){
                    reduced= 1;//青铜
                }else if (integer>=200&&integer<600){
                    reduced= 0.99;//白银
                }else if(integer>=600&&integer<1500){
                    reduced= 0.97;//黄金
                }else if (integer>=1500&&integer<3200){
                    reduced= 0.95;//珀金
                }else{
                    reduced= 0.90;//钻石
                }
            }else {
                reduced=1;
            }
        }
    return reduced;
}

//查询总金额
@RequestMapping("/checkoutPayment")
@ResponseBody
public double checkoutPayment(){
        double total=0;
    for (Map<String,Object> checklist:log4jService.checkoutSelect(null)
         ) {
        Double quantity= (Double) checklist.get("quantity");
        double sprice=Double.valueOf(String.valueOf((BigDecimal)checklist.get("sprice")));
        total+=quantity*sprice;
    }
    return total;
}

//    结算完成删除清单
@RequestMapping("/checkoutFinallyDel")
@ResponseBody
public String checkDel(@RequestParam("vid") String vid){
    int vin=0;
//        遍历总金额
        double total=0;
        for (Map<String,Object> checklist:log4jService.checkoutSelect(null)
        ) {
            Double quantity= (Double) checklist.get("quantity");
            Double sprice=Double.valueOf(String.valueOf((BigDecimal)checklist.get("sprice")));
            total+=quantity*sprice;
        }
        //查询vid修改积分
        List<Vip> vips=vipService.vipSelectPhone(vid);
        if (vips.size()==0){
            vips=vipService.vipSelectVid(Integer.valueOf(vid));
        }
        if (vips.size()==1){
            Integer id=vips.get(0).getVipaccounts().getVid();
            Integer integer=vips.get(0).getVipaccounts().getIntegral()+(int)total;
            String grade="青铜会员";
            if (integer<200){
                grade="青铜会员";
            }else if (integer>=200&&integer<600){
                grade="白银会员";
            }else if(integer>=600&&integer<1500){
                grade="黄金会员";
            }else if (integer>=1500&&integer<3200){
                grade="珀金会员";
            }else{
                grade="钻石会员";
            }
            vin=vipService.vipUpdintegral(id,integer,grade);
        }
    List<Map<String,Object>> checkoutList=log4jService.checkoutSelect(null);
    Integer cid=0;
    Integer a=0;
    //查询购物清单 ,添加记录，删除所有清单
    for (Map<String,Object> checkouts:checkoutList
         ) {
        List<Map<String,Object>> commoditylog4js=log4jService.commoditylogSelect(null,null,null,null,null);
        cid= (Integer) checkouts.get("cid");
        Commodity commoditys=commodityService.commodityFindByCid(cid).get(0);
        BigDecimal price=commoditys.getCprice();
        BigDecimal sprice=commoditys.getSprice();
        Double count= (Double) checkouts.get("quantity");
        String mlittle=commoditys.getMids().getMlittle();
        String hiredate=DateUtil.getCurrentDate();
        Commoditylog4j commoditylog4j=new Commoditylog4j(null,cid,price,sprice,count,mlittle,hiredate,0);
        int ci=log4jService.commoditylogInsert(commoditylog4j);
        int cd=log4jService.checkoutDelCid(cid);
        Double snum=(Double) commodityService.sellSelect(null,cid).get(0).get("snum");
        int cq=commodityService.sellUpdSnum(cid,snum-count);
        if (cq==1&&ci==1&&cd==1){
            a+=1;
        }
    }
    if (vin==1&&a==checkoutList.size()){
        return "ture";
    }else {
        return "false";
    }
}

//    查询销售记录
    @RequestMapping("/commoditylogList")
    @ResponseBody
    public String commoditylogList(Integer logid,String cname,String state){
        Integer cid=null;
        if (cname!=null){
            cid=commodityService.commodityFindAll(null,cname,null).get(0).getCid();
        }
        Integer state1=0;
        if (state=="售出"){
            state1=0;
        }else if (state=="退回"){
            state1=1;
        }else if(state==null){
            state1=null;
        }else{
            state1=2;
        }
        return JSON.toJSONString(LayuiTable.tableData(log4jService.commoditylogSelect(logid,cid,null,null,state1).size(),
                log4jService.commoditylogSelect(logid,cid,null,null,state1)), SerializerFeature.DisableCircularReferenceDetect);
    }

    //基础面积图
    @RequestMapping("/Records")
    @ResponseBody
    public List<Map<String,Object>> Records() {
        return log4jService.commoditylogRecords();
    }

    //玫瑰图
    @RequestMapping("/Pies")
    @ResponseBody
    public List<Map<String,Object>> Pies() {
        return log4jService.commoditylogPies();
    }
}
